﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// FormAccess 的摘要说明
/// </summary>
public class FormAccess
{
	public FormAccess()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public static string GenerateID(string table, string key, string type)
    {
        string current_order_id, new_order_id, sqlstr, new_id;
        int ID_max, ID_count;
        DateTime dt = DateTime.Now;
        string date = string.Format("{0:yyyyMMdd}", dt);
        new_order_id = type + date;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShop"].ConnectionString);
        conn.Open();
        sqlstr = "select count(" + key + ") from " + table + " Where substring(" + key + ",5,8) = '" + date + "'";
        SqlCommand sqlcom = new SqlCommand(sqlstr, conn);
        ID_count = (int)sqlcom.ExecuteScalar();
        if (ID_count != 0)
        {
            //sqlstr = "select max(AllocationID) from AllocationDetail Where substring(AllocationID,5,8) = '" + date + "'";
            sqlstr = "select max(" + key + ") from " + table + " Where substring(" + key + ",5,8) = '" + date + "'";
            SqlCommand sqlcom2 = new SqlCommand(sqlstr, conn);
            current_order_id = sqlcom2.ExecuteScalar().ToString();
            ID_max = Convert.ToInt32(current_order_id.Substring(13, 3));
            ID_max++;
            if (ID_max.ToString().Trim().Length == 1)
            {
                new_id = "00" + ID_max.ToString();
            }
            else if (ID_max.ToString().Trim().Length == 2)
            {
                new_id = "0" + ID_max.ToString();
            }
            else
            {
                new_id = ID_max.ToString();
            }
            new_order_id += new_id;
        }
        else
        {
            new_order_id += "001";
        }
        conn.Close();
        return new_order_id;
    }

    public static DataSet GetFilteredSet(string sqlstr)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShop"].ConnectionString);
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
        sda.Fill(ds);
        conn.Close();
        return ds;
    }
}
